home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Venus 3.5 / EventHandlers.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-02  |  4.3 KB  |  134 lines  |  [TEXT/CWIE]

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *
  5.  *                        Event Handler Classes
  6.  *                            Implementation
  7.  *        
  8.  *
  9.  ***********************************************************************
  10.  */
  11.  
  12. #include "EventHandlers.h"
  13. #include "window.h"
  14. #include <SIOUX.h>
  15.  
  16. #if 0
  17.                                     // Pointer to a null event handler on the top
  18.                                     // of the chain of registered handlers.
  19.                                     // For now we don't have a chain, actually
  20.                                     // so this points to the only one event handler,
  21.                                     // or nil
  22. NullEventHandler * NullEventHandler::first_handler = nil;
  23. #endif
  24.  
  25. bool EventHandler::putback_pending = false;
  26. EventRecord EventHandler::putback_event;
  27.  
  28.                             // Un-get an event. The 'event' becomes the first event
  29.                             // returned by EventHandler::wait_next_event() or in the
  30.                             // EventHandler::loop()
  31. void EventHandler::put_back(const EventRecord& event)
  32. {
  33.   assert( !putback_pending );
  34.   putback_event = event;
  35.   putback_pending = true;
  36. }
  37.  
  38.                             // A shorthand of put_back() if I want to create a
  39.                             // 'myevent' and push it back into the "event queue"
  40. void EventHandler::put_back_my_event
  41.   (const EventModifiers modifiers, const UInt32 message, const Point where)
  42. {
  43.   assert( !putback_pending );
  44.   putback_event.what = mykind;
  45.   putback_event.message = message;
  46.   putback_event.when = 0;
  47.   putback_event.where = where;
  48.   putback_event.modifiers = modifiers;
  49.   putback_pending = true;
  50. }
  51.  
  52. void EventHandler::put_back_my_event(const EventModifiers modifiers, const UInt32 message)
  53. {
  54.   assert( !putback_pending );
  55.   putback_event.what = mykind;
  56.   putback_event.message = message;
  57.   putback_event.when = 0;
  58. //  putback_event.where = where;
  59.   putback_event.modifiers = modifiers;
  60.   putback_pending = true;
  61. }
  62.  
  63.  
  64.                                       // A wrap around WaitNextEvent() to take care
  65.                                       // of put-back events, and to transparently
  66.                                       // process high-level events
  67. bool EventHandler::wait_next_event(EventRecord& event, const int timeout)
  68. {
  69.   if( putback_pending )
  70.     putback_pending = false, event = putback_event;
  71.   else
  72.     if( !WaitNextEvent(everyEvent, &event, timeout, nil) )
  73.       return false;                        // That was a null-event
  74.   
  75.   if( event.what == kHighLevelEvent )
  76.   {    
  77.     OSErr error = AEProcessAppleEvent(&event);
  78.     if( error != errAEEventNotHandled )
  79.       do_well(error);
  80.     return wait_next_event(event,timeout);        // Done with this event, wait for another
  81.    }
  82.    
  83.                                                // If there is a SIOUX console window around,
  84.                                                // check to see if it claims the event
  85.                                                // We must give SIOUX a chance to handle its
  86.                                                // update/activate events (which would be
  87.                                                // generated for the SIOUX window when we open
  88.                                                // our window or change the color environment)
  89.                                                // Otherwise, our update-event based animation
  90.                                                // won't work
  91.    extern WindowPtr SIOUXTextWindow;
  92.    if( ((WindowPtr)event.message == SIOUXTextWindow) && SIOUXHandleOneEvent(&event) )
  93.      return wait_next_event(event,timeout);    // Done with this event, wait for another
  94.    return true;
  95. }
  96.   
  97.  
  98.  
  99.                 // Event handling loop. Returns only when the
  100.                 // WindowObject is to be destroyed
  101.                 // Performs a very basic dispatching of a received event
  102. void EventHandler::loop(void)
  103. {
  104.   for(;;)
  105.   {
  106.     if( !wait_next_event(the_event,event_timeout) )
  107.       if( serviced_window.handle_null_event(the_event.when) )
  108.          continue;
  109.        else
  110.          return;                // Null event handler decided it's time to quit
  111.   
  112.      if( !serviced_window.handle_event(the_event) )
  113.        return;                // The handler decided it doesn't want any more events
  114.    }
  115. }
  116.  
  117.                             // Flush incidental events: mainly, take care of 
  118.                             // update/activate/etc events that might be pending on
  119.                             // a SIOUX console if any
  120.                             // Pending update events on windows other than our main
  121.                             // map window interferes with the way we do animation
  122.                             // (based on a set of update events)
  123.                             // Note that update/activate events are not actually
  124.                             // placed in a OS even queue, but genereated by the
  125.                             // toolbox for the window which happens to be on the front
  126. void EventHandler::flush_side_events(void)
  127. {
  128.   EventRecord the_event;
  129.   while( wait_next_event(the_event,0) && SIOUXHandleOneEvent(&the_event) )
  130.     ;
  131.   if( the_event.what != nullEvent )
  132.     put_back(the_event);
  133. }
  134.